import {
    GLOBAL_STORE_TYPE_STORAGE,
    GLOBAL_STORE_KEY_CURRENT_LESSON,
    GLOBAL_STORE_KEY_CURRENT_PLAY_LIST,
    GLOBAL_STORE_KEY_STUDIED_LESSONS
} from '../../common/constants';

import {
    setGlobalStore,
    getGlobalStore,
    removeGlobalStore
} from '../../utils/global-data';

/**
 * 将课节信息中与音频播放器的字段，转换成播放器都够识别的字段
 * @param {Object} lessonInfo 课节信息
 */
exports.lessonToAudio = (lessonInfo) => {
    let isEmpty = true;
    if (!lessonInfo) {
        return;
    }

    // 判断是否为空对象
    for (let key in lessonInfo) {
        if (lessonInfo.hasOwnProperty(key)) {
            isEmpty = false;
            break;
        }
    }

    if (isEmpty) {
        return;
    }

    let obj = JSON.parse(JSON.stringify(lessonInfo));
    // obj.title = '';
    obj.singer = '';
    obj.coverImgUrl = obj.courseSharePic || '';
    obj.src = obj.src || obj.audioUrl;
    return obj;
};

/**
 * 获取当前正在学习并且包含音频的课程
 */
exports.getCurrentAudioLesson = () => {
    let currentLesson = getCurrentLesson({
        isSync: true
    });

    // TODO 判断课程是否包含音频，如果没有音频不返回课程信息
    // currentLesson = null;
    return currentLesson;
};

/**
 * 将当前正在学习的课程信息放入全局缓存中
 * @param {object} value 当前播放的课程信息
 * @param {*} opts 操作选项
 */
export const setCurrentLesson = function (value, opts) {
    return setGlobalStore(GLOBAL_STORE_KEY_CURRENT_LESSON, value, opts);
};

export const getCurrentLesson = function (opts) {
    return getGlobalStore(GLOBAL_STORE_KEY_CURRENT_LESSON, opts);
};

/**
 * 删除当前正在学习的课程数据
 * @param {*} opts 选项
 */
export const removeCurrentLesson = function (opts) {
    return removeGlobalStore(GLOBAL_STORE_KEY_CURRENT_LESSON, { isSync: true });
};

/**
 * 向全局数据缓存中添加当前播放列表
 * @param {*} value 播放列表
 * @param {*} opts 操作选项
 */
export const setCurrentPlayList = function (value, opts) {
    return setGlobalStore(GLOBAL_STORE_KEY_CURRENT_PLAY_LIST, value, opts);
};

/**
 * 从全局数据缓存中获取当前播放列表
 * @param {*} opts 操作选项
 * @return Promise
 */
export const getCurrentPlayList = (opts) => {
    return getGlobalStore(GLOBAL_STORE_KEY_CURRENT_PLAY_LIST, opts);
};

/**
 * 将课程添加已经学过的课程列表
 * @param {*} lessonInfo 课节数据对象
 */
export const setStudiedLesson = (lessonInfo) => {
    let studiedLessons = getGlobalStore(GLOBAL_STORE_KEY_STUDIED_LESSONS, {
        storageType: GLOBAL_STORE_TYPE_STORAGE,
        isSync: true
    });

    if (!studiedLessons) {
        studiedLessons = [];
    }

    studiedLessons.unshift(lessonInfo);

    if (studiedLessons.length > 10) {
        studiedLessons.length = 10;
    }

    setGlobalStore(GLOBAL_STORE_KEY_STUDIED_LESSONS, studiedLessons, {
        storageType: GLOBAL_STORE_TYPE_STORAGE,
        isSync: true
    });
};

/**
 * 从已经学过的课程列表缓存中，获取与lessonId匹配的课节信息
 *
 * 1、如果没有与lessonId匹配的课节信息，返回undefined
 * 2、如果没有传lessonId，则返回所有的已经缓存的课节信息
 * @param {number} lessonId 课节id
 */
export const getStudiedLesson = (lessonId) => {
    let result;
    let studiedLessons = getGlobalStore(GLOBAL_STORE_KEY_STUDIED_LESSONS, {
        storageType: GLOBAL_STORE_TYPE_STORAGE,
        isSync: true
    });

    if (lessonId) {
        result = studiedLessons.find(item => {
            return item.id === lessonId;
        });
    } else {
        result = studiedLessons;
    }

    return result;
};

/**
 * 清空已经学习过的课程列表
 */
export const removeStudiedLesson = () => {
    return removeGlobalStore(GLOBAL_STORE_KEY_STUDIED_LESSONS, {
        storageType: GLOBAL_STORE_TYPE_STORAGE,
        isSync: true
    });
};

/**
 * 根据课程ID，获取课程在播放列表中的索引
 * @param {*} lessonId 课节id
 * @returns 课节在课节列表中的索引
 */
const getIndexInPlayList = (lessonId) => {
    if (!lessonId) {
        throw new Error('课程id不能空');
    }

    let index = -2;
    let playList = getCurrentPlayList({
        isSync: true
    });

    if (playList && Array.isArray(playList)) {
        index = playList.findIndex(item => {
            return item.lessonId === lessonId;
        });
    }
    return {
        index,
        playList
    };
};

/**
 * 从缓存的可播放列表中，获取当前播放音频的上一首音频信息
 * @param {number} currentLessonId 当前正在播放课程id
 * @returns 如果存在则返回lesson对象，如果没有返回undefined
 */
export const getPrevInfo = (currentLessonId) => {
    let lessonInfo;
    let { index, playList } = getIndexInPlayList(currentLessonId);

    if (index > 0 && playList) {
        lessonInfo = playList[index - 1];
    }

    return lessonInfo;
};

/**
 * 从缓存的可播放列表中，获取当前播放音频的下一首音频信息
 * @param {number} currentLessonId 当前正在播放课程id
 * @returns 课程对象
 */
export const getNextInfo = (currentLessonId) => {
    let lessonInfo;
    let { index, playList } = getIndexInPlayList(currentLessonId);

    if (index >= 0 && playList && (playList.length - index >= 2)) {
        lessonInfo = playList[index + 1];
    }

    return lessonInfo;
};

/**
 * 清理与课程有关的全局数据
 */
export const cleanCourseGlobalData = () => {
    console.log('清理与课程有关的全局缓存数据');
    removeCurrentLesson();
    removeStudiedLesson();
};
